//
//  JXAddressDynamicViewController.m
//  JXAddressApp
//
//  Created by huawei.zheng on 2018/12/3.
//  Copyright © 2018 zhhw_home. All rights reserved.
//

#import "JXAddressDynamicViewController.h"
#import <QMUIKit/QMUICommonDefines.h>
#import "EgovaNetworking.h"
#import "JXNewModel.h"
#import "JXNewsTableViewCell.h"
#import "Masonry.h"
#import <UIColor+QMUI.h>
#import "UIImageView+AFNetworking.h"
#import "JXBaseWKWebViewController.h"
#import <QMUIKit/NSString+QMUI.h>


@interface JXAddressDynamicViewController ()
<
UITableViewDelegate,
UITableViewDataSource
>

@property (nonatomic, strong) UITableView      *tableView;

@property (nonatomic, strong) NSArray           *models;

@end

@implementation JXAddressDynamicViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self initRootView];
    [self initdt];
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
}

- (void)initRootView
{
    self.view.backgroundColor = [UIColor qmui_colorWithHexString:@"#EAEAEA"];
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.tableFooterView = [UIView new];
    [self.view addSubview:_tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    [_tableView registerClass:[JXNewsTableViewCell class] forCellReuseIdentifier:@"JXNewsTableViewCell"];
}

- (void)initdt
{
    NSMutableArray *list = [NSMutableArray new];
    NSString *url = @"http://www.wegis.net/demo/home/baseinfo/getserviceannouncementregion";
    [EgovaNetworking getWithUrl:url cache:NO completed:^(NSInteger code, id data, NSString *msg) {
        NSLog(@"%@", data);
        NSArray *values = nil;
        if (code == 0 && data[@"data"]) {
            values = data[@"data"];
        }
        else {
            values = [[self class] defaultDt];
        }
        
        for (NSDictionary *item in values) {
            JXNewModel *model = [[JXNewModel alloc] init];
            model.nid = [item[@"id"] integerValue];
            model.filename = item[@"filename"];
            model.fullname = item[@"fullname"];
            model.publishtime = item[@"publishtime"];
            model.type = [item[@"type"] integerValue];
            model.flag = [item[@"flag"] integerValue];
            
            [list addObject:model];
        }
        
        self.models = [list copy];
        [self.tableView reloadData];
    }];
}

+(NSArray*)defaultDt
{
    return @[
             @{
                 @"id": @(1),
                 @"filename": @"关于规范和加强地名管理的通知",
                 @"fullname": @"1$关于规范和加强地名管理的通知.doc",
                 @"fileid": @"",
                 @"publishtime": @"2018-06-05",
                 @"type": @(4),
                 @"flag": @(0)
                 },
             @{
                 @"id": @(2),
                 @"filename": @"关于整顿规范地名标志问题的紧急通知",
                 @"fullname": @"2$关于整顿规范地名标志问题的紧急通知.doc",
                 @"fileid": @"",
                 @"publishtime": @"2018-06-05",
                 @"type": @(4),
                 @"flag": @(0)
                 },
             @{
                 @"id": @(3),
                 @"filename": @"街道办事处组织条例",
                 @"fullname": @"3$街道办事处组织条例.doc",
                 @"fileid": @"",
                 @"publishtime": @"2018-06-05",
                 @"type": @(5),
                 @"flag": @(0)
                 },
             @{
                 @"id": @(8),
                 @"filename": @"武汉市街道办事处条例",
                 @"fullname": @"8$武汉市街道办事处条例.pdf",
                 @"fileid": @"",
                 @"publishtime": @"2018-06-05",
                 @"type": @(5),
                 @"flag": @(0)
                 }
             ];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _models.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JXNewModel *model = [_models objectAtIndex:indexPath.row];
    
    JXNewsTableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"JXNewsTableViewCell"];
    cell.titleLabel.text = model.filename;
    cell.dateLabel.text = [[NSString alloc] initWithFormat:@"时间:%@", model.publishtime];
    [cell.imgView setImageWithURL:[NSURL URLWithString:@"http://www.wegis.net/demo/style/jxgeocensus/common/css/images/desktop/demo1_2.png"]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    JXNewModel *model = [_models objectAtIndex:indexPath.row];
    
    NSString *fileName = [[model.fullname stringByReplacingOccurrencesOfString:@"doc" withString:@"pdf"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSString *fileurl = [[NSString alloc] initWithFormat:@"http://www.wegis.net/demo/pdf/policyfilepdf/%@", fileName];
    
    JXBaseWKWebViewController *vc = [JXBaseWKWebViewController new];
    vc.webUrl = [NSURL URLWithString:fileurl];
    vc.webVCTitle = model.filename;
    vc.hidesBottomBarWhenPushed = YES;
    [self.parentViewController.navigationController pushViewController:vc animated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

- (NSString *)URLEncodedString:(NSString*)url
{
    NSString *unencodedString = url;
    NSString *encodedString = (NSString *)
    CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                              (CFStringRef)unencodedString,
                                                              NULL,
                                                              (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                              kCFStringEncodingUTF8));
    
    return encodedString;
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
